//
//  UIAlertView+TLBlock.m
//  tlAlertView
//
//  Created by telen on 15/12/29.
//  Copyright © 2015年 刘赞黄Telen. All rights reserved.
//

#import "UIAlertView+TLBlock.h"
#import "NSObject+Associated.h"

@implementation UIAlertView_Models
@end

@interface UIAlertView()
@property(nonatomic,strong)TLAlertView_didClick didClick;
@property(nonatomic,strong)NSArray* clickModels;
@end

@implementation UIAlertView(TLBlock)

ASSOCIATED(didClick, setDidClick, TLAlertView_didClick, OBJC_ASSOCIATION_RETAIN);
ASSOCIATED(clickModels, setClickModels, NSArray<UIAlertView_Models*>*, OBJC_ASSOCIATION_RETAIN);

static NSUInteger tl_isAlertShow = 0;

+ (instancetype)tl_showTitle:(NSString *)title message:(NSString *)message didClick:(TLAlertView_didClick)clicked buttonTitles:(nullable NSString *)otherButtonTitles, ...
{
    NSString* btn[10];
    int i = 0;
    va_list params;//定义一个指向个数可变的参数列表指针
    va_start(params, otherButtonTitles);//va_start 得到第一个可变参数地址
    btn[i] = otherButtonTitles;
    if (otherButtonTitles) {
        NSString* arg = nil;
        i++;
        while ((arg = va_arg(params, NSString *))) {
            if (arg) {
                btn[i]= arg;
            }
            i++;
        }
    }
    va_end(params);
    //
    UIAlertView* alert = [self alloc];
    alert = [alert initWithTitle:title message:message delegate:alert cancelButtonTitle:nil otherButtonTitles:btn[0],btn[1],btn[2],btn[3],btn[4],btn[5],btn[6],btn[7],btn[8],btn[9],nil];
    alert.didClick = clicked;
    tl_isAlertShow++;
    [alert show];
    return alert;
}

+ (instancetype)tl_showTitle:(NSString *)title message:(NSString *)message willShow:(TLAlertView_willShow)willshow didClick:(TLAlertView_didClick)clicked buttonTitles:(nullable NSString *)otherButtonTitles, ...
{
    NSString* btn[10];
    int i = 0;
    va_list params;//定义一个指向个数可变的参数列表指针
    va_start(params, otherButtonTitles);//va_start 得到第一个可变参数地址
    btn[i] = otherButtonTitles;
    if (otherButtonTitles) {
        NSString* arg = nil;
        i++;
        while ((arg = va_arg(params, NSString *))) {
            if (arg) {
                btn[i]= arg;
            }
            i++;
        }
    }
    va_end(params);
    //
    UIAlertView* alert = [self alloc];
    alert = [alert initWithTitle:title message:message delegate:alert cancelButtonTitle:nil otherButtonTitles:btn[0],btn[1],btn[2],btn[3],btn[4],btn[5],btn[6],btn[7],btn[8],btn[9],nil];
    alert.didClick = clicked;
    if (willshow)willshow(alert);
    tl_isAlertShow++;
    [alert show];
    return alert;
}

#pragma mark
+ (instancetype)tl_showTitle:(NSString *)title message:(NSString *)message models:(nullable NSArray<UIAlertView_Models *> *)btns
{
    NSString* btn[10];
    for (int i=0; i<10 && i<btns.count; i++) {
        btn[i] = btns[i].btnTitle;
    }
    //
    UIAlertView* alert = [self alloc];
    alert = [alert initWithTitle:title message:message delegate:alert cancelButtonTitle:nil otherButtonTitles:btn[0],btn[1],btn[2],btn[3],btn[4],btn[5],btn[6],btn[7],btn[8],btn[9],nil];
    alert.clickModels = btns;
    tl_isAlertShow++;
    [alert show];
    return alert;
}

#pragma mark
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (tl_isAlertShow > 0) {
        tl_isAlertShow--;
    }
    if (self.didClick) {
        self.didClick(alertView,buttonIndex);
    }
    if (self.clickModels && buttonIndex < self.clickModels.count) {
        UIAlertView_Models* model = self.clickModels[buttonIndex];
        if(model.blockCase)model.blockCase();
    }
}

#pragma mark-
+ (BOOL)isAlertShow
{
    return tl_isAlertShow > 0;
}

+ (NSUInteger)countShowing
{
    return tl_isAlertShow;
}

@end
